package utils

import (
	"errors"

	guuid "github.com/google/uuid"
)

// 使用帮助
//
// 使用 NewUUID().String() 来产生uuid

type UUID guuid.UUID

func IsValidUUID(u string) bool {
	_, err := guuid.Parse(u) //todo 待优化
	return err != nil
}

func NewUUID() UUID {
	return (UUID)(guuid.New())
}

func UUIDFromBytes(b []byte) (UUID, error) {
	if len(b) != 16 {
		return UUID{}, errors.New("uuid byte len is not 16")
	}
	var u UUID
	for i := range b {
		u[i] = b[i]
	}
	return u, nil
}

func UUIDParseFromString(s string) (UUID, error) {
	u, err := guuid.Parse(s)
	return (UUID)(u), err
}

func (u UUID) Bytes() []byte {
	return u[:]
}

func (u UUID) String() string {
	return (guuid.UUID)(u).String()
}
